package com.party.core.service.commune.impl;


import com.google.common.collect.Maps;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.commune.CMemberIntegralBalanceLog;
import com.party.core.model.commune.CMemberIntegralCount;
import com.party.core.service.commune.ICMemberIntegralBalanceLogService;
import com.party.core.service.commune.ICMemberIntegralCountService;
import com.party.core.service.commune.ICUserIntegralService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @program: party
 * @description:
 * @author: Hua Qunhao
 * @create: 2020-04-10 12:11
 **/
@Service
@Transactional
public class CMemberIntegralBizService {
    protected static Logger logger = LoggerFactory.getLogger(CMemberIntegralBizService.class);

    @Autowired
    private ICMemberIntegralBalanceLogService memberIntegralBalanceLogService;
    @Autowired
    private ICMemberIntegralCountService memberIntegralCountService;
    @Autowired
    private ICUserIntegralService userIntegralService;

    private ExecutorService threadPool = Executors.newFixedThreadPool(50);

    public void refreshCMemberIntegralCount(List<String> memberIds) {
        Map<String, Object> params = Maps.newHashMap();
        if (memberIds != null && memberIds.size() > 0) {
            params.put("memberIds", memberIds);
        }
        List<Map<String, Object>> integralSumList = memberIntegralBalanceLogService.getIntegralSum(params);
        for (Map<String, Object> mmp : integralSumList) {
            String memberId = (String) mmp.get("memberId");
            Double addIntegral = (Double) mmp.get("addIntegral");
            Double reduceIntegral = (Double) mmp.get("reduceIntegral");
            CMemberIntegralCount cMemberIntegralCount = memberIntegralCountService.getByMemberId(memberId);
            if (cMemberIntegralCount == null) {
                cMemberIntegralCount = new CMemberIntegralCount();
                cMemberIntegralCount.setMemberId(memberId);
                cMemberIntegralCount.setTotalIntegral(addIntegral);
                cMemberIntegralCount.setConsumedIntegral(reduceIntegral);
                cMemberIntegralCount.setAvailableIntegral(BigDecimalUtils.sub(addIntegral, reduceIntegral));
                memberIntegralCountService.insert(cMemberIntegralCount);
            } else {
                cMemberIntegralCount.setTotalIntegral(addIntegral);
                cMemberIntegralCount.setConsumedIntegral(reduceIntegral);
                cMemberIntegralCount.setAvailableIntegral(BigDecimalUtils.sub(addIntegral, reduceIntegral));
                memberIntegralCountService.update(cMemberIntegralCount);
            }
        }
    }

    public void refreshCMemberIntegralBalanceLog(List<String> memberIds) {
        Map<String, Object> params = Maps.newHashMap();
        if (memberIds != null && memberIds.size() > 0) {
            params.put("memberIds", memberIds);
        }
        List<CMemberIntegralBalanceLog> cMemberIntegralBalanceLogs = userIntegralService.queryUserIntegralAll(params);
        for (CMemberIntegralBalanceLog cmibOrg : cMemberIntegralBalanceLogs) {
            if (StringUtils.isNotEmpty(cmibOrg.getTitle())) {
                CMemberIntegralBalanceLog bySourceId = memberIntegralBalanceLogService.getBySourceId(cmibOrg.getSourceId());
                if (bySourceId == null) {
                    memberIntegralBalanceLogService.insert(cmibOrg);
                } else {
                    bySourceId.setSource(cmibOrg.getSource());
                    bySourceId.setIntegral(cmibOrg.getIntegral());
                    bySourceId.setTitle(cmibOrg.getTitle());
                    bySourceId.setDelFlag(cmibOrg.getDelFlag());
                    memberIntegralBalanceLogService.update(bySourceId);
                }
            }
        }
    }

    public void refreshCMemberIntegral(List<String> memberIds) {
        final  List<String> memberIds_final = memberIds;
        refreshCMemberIntegralBalanceLog(memberIds_final);
        refreshCMemberIntegralCount(memberIds_final);
/*        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                refreshCMemberIntegralBalanceLog(memberIds_final);
                refreshCMemberIntegralCount(memberIds_final);
            }
        });*/
    }
}
